//
//  AboutViewController.m
//  Assistant
//
//  Created by Hunk on 12-3-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AboutViewController.h"
#import "AboutCell.h"

@implementation AboutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [_tableView release];
    
    [super dealloc];
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"关于", nil);
    
    self.navigationController.navigationBar.tintColor = TINT_COLOR;
    // BackgroundImageView
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0)];
    [backgroundImageView setImage:[UIImage imageNamed:@"background.png"]];
    [self.view addSubview:backgroundImageView];
    [backgroundImageView release];
    
    // LeftBarButtonItem
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"取消", nil)
                                                                          style:UIBarButtonItemStyleBordered
                                                                         target:self
                                                                         action:@selector(cancelButtonHandle:)];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    [leftBarButtonItem release];
    
    // Init tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0) 
                                              style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:[UIColor clearColor]];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
        {
            return 2;
        }
            break;
        case 1:
        {
            return 4;
        }
            break;
        default:
            break;
    }
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(1 == section)
    {
        return NSLocalizedString(@"更多应用", nil);
    }
    else
    {}
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AboutCell *cell = nil;
    
    switch (indexPath.section)
    {
        case 0:
        {
            static NSString *aboutCellIdentifier = REUSEIDENTIFIER_0;
            
            cell = [tableView dequeueReusableCellWithIdentifier:aboutCellIdentifier];
            
            if(nil == cell)
            {
                cell = [[[AboutCell alloc] initWithStyle:UITableViewCellStyleDefault                                
                                         reuseIdentifier:aboutCellIdentifier] autorelease];
            }
            
            [cell setIndexPath:indexPath];
        }
            break;
        case 1:
        {
            static NSString *aboutCellIdentifier = REUSEIDENTIFIER_1;
            
            cell = [tableView dequeueReusableCellWithIdentifier:aboutCellIdentifier];
            
            if(nil == cell)
            {
                cell = [[[AboutCell alloc] initWithStyle:UITableViewCellStyleDefault                                
                                         reuseIdentifier:aboutCellIdentifier] autorelease];
            }
            
            [cell setIndexPath:indexPath];
        }
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch(indexPath.section) 
    {
        case 0:
        {
            if(0 == indexPath.row)
            {
                // 评价
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:RATING_ADDRESS]];
            }
            else
            {
                // 意见&建议
                [self performSelector:@selector(sendEMail) withObject:nil afterDelay:0.1];
            }
        }
            break;
        case 1:
        {
            NSString *urlString = nil;
            
            switch(indexPath.row) 
            {
                case 0: // 短信祝福
                {
                    urlString = FESTIVAL_SMS_ADDRESS;
                }
                    break;
                case 1: // 驾照模拟考试
                {
                    urlString = DRIVER_ADDRESS;
                }
                    break;
                case 2: // 宝宝乖乖睡觉
                {
                    urlString = AUDIOBOOKS_ADDRESS;
                }
                    break;
                case 3: // 团游世界
                {
                    urlString = TUANYOUWORLD_ADDRESS;
                }
                    break;
                default:
                    break;
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 
#pragma mark Cancel button handle
- (void)cancelButtonHandle:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}
      
#pragma mark -
#pragma mark Send email
- (void)sendEMail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        if([mailClass canSendMail])
        {   
            // MFMailComposeViewController
            MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
            [mailComposeViewController setMailComposeDelegate:self];
            [mailComposeViewController setToRecipients:[NSArray arrayWithObject:EMAIL_FEEDBACK]];
            [mailComposeViewController setSubject:NSLocalizedString(@"意见&建议", nil)];
            [mailComposeViewController setMessageBody:NSLocalizedString(@"<点击此处进行信息输入>\n\n\n感谢您对<通讯小秘>提出宝贵意见和建议", nil) 
                                               isHTML:NO];
            [self presentModalViewController:mailComposeViewController animated:YES];
            [mailComposeViewController release];
        }
        else
        {
            // 如果没有绑定邮箱
			NSString* strRecipient = [NSString stringWithFormat:@"mailto:%@?subject=%@", EMAIL_FEEDBACK, NSLocalizedString(@"意见&建议", nil)];
			
			// Mail body
			NSString* strBody = [NSString stringWithFormat:@"&body=%@", NSLocalizedString(@"<点击此处进行信息输入>\n\n\n感谢您对<通讯小秘>提出宝贵意见和建议", nil)];
			
			NSString* strEmail = [NSString stringWithFormat:@"%@%@", strRecipient, strBody];
			strEmail = [strEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
			
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strEmail]];
        }
    }
    else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"抱歉", nil)
                                                        message:NSLocalizedString(@"您的设备不支持发邮件功能!", nil)
                                                       delegate:nil 
                                              cancelButtonTitle:NSLocalizedString(@"好的", nil)
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

#pragma mark -
#pragma mark MFMailComposeViewController delegate method
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
